package admin;

import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import beans.AdminPageBean;
import utils.TechresourceDAORemote;
import dtos.TechresourceDTO;
import dtos.UserDTO;

/**
 * Java bean for modifying the ip of printers.
 * 
 * @author Szekely Krisztian-Vilmos
 * 
 */
@ManagedBean(name = "printerIpBean")
@ViewScoped
public class PrinterIpBean {
	@EJB
	TechresourceDAORemote tr;

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());

	@ManagedProperty(value = "#{userDTO}")
	private UserDTO user;

	private List<TechresourceDTO> listPrinters;

	private String action;
	private String message;
	private String msgSucess = bundle
			.getString("Beans_admin_printerIpBean_msgSucess");

	@ManagedProperty("#{adminPageBean}")
	AdminPageBean apb;

	public AdminPageBean getApb() {
		return apb;
	}

	public void setApb(AdminPageBean apb) {
		this.apb = apb;
	}

	/**
	 * Retrieve a list of printers from the users domain.
	 */
	@PostConstruct
	private void init() {
		user = (UserDTO) FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("loggedUser");
		getAllPrinters();
		action = "view";
		message = "";
	}

	public UserDTO getUser() {
		user = (UserDTO) FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("loggedUser");
		return user;
	}

	public void setUser(UserDTO user) {
		this.user = user;
	}

	public List<TechresourceDTO> getListPrinters() {
		return listPrinters;
	}

	public void setListPrinters(List<TechresourceDTO> listPrinters) {
		this.listPrinters = listPrinters;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * The list of printers is refreshed from the database.
	 */
	private void getAllPrinters() {
		listPrinters = tr.getTechResourcesDTOPrinters();
	}

	/**
	 * Sets the value of the variable being used to render components in xhtml
	 * page. We wish to perform an edit.
	 */
	public void editCommand() {
		action = "edit";
		message = "";
	}

	/**
	 * Sets the value of the variable being used to render components in xhtml
	 * page. We want to cancel editing the ip.
	 */
	public void cancelCommand() {
		action = "view";
		message = "";
	}

	/**
	 * Attributes are being retrieved from the from, and if they are in order,
	 * then the update is performed. After updating, the list of tech resources
	 * is refreshed. Sets the value of the variable being used to render
	 * components in xhtml page. We have finished editing the printers ip.
	 */
	public void saveCommand(ActionEvent event) {
		int id = (int) event.getComponent().getAttributes().get("printerId");
		String ip = (String) event.getComponent().getAttributes()
				.get("printerIp").toString();

		TechresourceDTO printer = tr.getTechresourcesById(id);
		printer.setIp(ip);
		tr.updateTechresource(printer);

		getAllPrinters();
		action = "view";
		message = msgSucess;
	}

	public String getMsgSucess() {
		return msgSucess;
	}

	public void setMsgSucess(String msgSucess) {
		this.msgSucess = msgSucess;
	}
}
